/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * analisis.java
 *
 * Created on 02 Apr 14, 5:46:35
 */
package sample;
import java.text.DecimalFormat;


/**
 *
 * @author ACER ASPIRE 4741Z
 */
public class analisis extends javax.swing.JFrame {

    private Database database;
    private float[] dataPemilih;
    private float[] minSupport;
    private String[][] dataSupport;
    private float[] kombinasi;
    private float[][] confidende;
    private String[][] prodiKombinasi;
    private String[][] dataConfidence;
    private String[][] dataFinal;
    
    
    
    /** Creates new form analisis */
    public analisis() {
        initComponents();
    }

    public void setDataPemilih() {

        for (int i = 0; i < dataPemilih.length; i++) {
            dataPemilih[i] = Float.parseFloat(database.getProdi()[i][2]) / (float) database.getPendaftar().length;
            dataPemilih[i] *= 100;

        }

    }

    public void setMinSupport() {
        float min = Float.parseFloat(txtMin.getText());
        int jumlah = 0;
        for (int i = 1; i < dataPemilih.length; i++) {
            if (dataPemilih[i] >= min) {
                jumlah += 1;
            }
        }
        dataSupport = new String[jumlah][2];

        int x = 0;
        for (int i = 1; i < dataPemilih.length; i++) {
            if (dataPemilih[i] >= min) {
                dataSupport[x][0] = String.valueOf(dataPemilih[i]);
                dataSupport[x][1] = database.getProdi()[i][0];
                x++;
            }
        }
    }

    public void setKombinasi() {
        kombinasi = new float[(int) banyakKombinasi(dataSupport.length)];
        int chance = (int) banyakKombinasi(dataSupport.length);
        int peserta = (int) database.getPendaftar().length;
        int prodi = dataSupport.length;
        prodiKombinasi = new String[chance][5];
        int a = 0;
        int b = 0;
        for (int i = 0; i < chance; i++) {
            kombinasi[i] = 0;
            for (int j = 0; j < peserta; j++) {
                if ((database.getPendaftar()[j][2].equals(dataSupport[b][1])
                        && database.getPendaftar()[j][3].equals(dataSupport[a + 1][1]))
                        || (database.getPendaftar()[j][3].equals(dataSupport[b][1])
                        && database.getPendaftar()[j][2].equals(dataSupport[a + 1][1]))) {
                    kombinasi[i] += 1;
                }

            }
            prodiKombinasi[i][2] = String.valueOf(kombinasi[i]);
            prodiKombinasi[i][3] = dataSupport[b][1];
            prodiKombinasi[i][4] = dataSupport[a + 1][1];
            if (a >= (prodi - 2)) {
                b = b + 1;
                a = b;
            } else {
                a++;
            }

        }
        int cek = 0;
    }

    public void setConfidence() {
        confidende = new float[(int) banyakKombinasi(dataSupport.length)][2];

        int chance = (int) banyakKombinasi(dataSupport.length);
        for (int i = 0; i < chance; i++) {
            confidende[i][0] = kombinasi[i] / getSupport(prodiKombinasi[i][3]);
            confidende[i][0] *= 100;
            confidende[i][1] = kombinasi[i] / getSupport(prodiKombinasi[i][4]);
            confidende[i][1] *= 100;
            prodiKombinasi[i][0] = String.valueOf(confidende[i][0]);
            prodiKombinasi[i][1] = String.valueOf(confidende[i][1]);
        }

    }

    public void setMinConfidence() {
        float min = Float.parseFloat(txtConf.getText());
        int jumlah = 0;
        for (int i = 0; i < prodiKombinasi.length; i++) {
            if (Float.parseFloat(prodiKombinasi[i][0]) >= min || Float.parseFloat(prodiKombinasi[i][0]) >= min) {
                jumlah += 1;
            }
        }
        dataConfidence = new String[jumlah][5];
        int x = 0;
        for (int i = 0; i < prodiKombinasi.length; i++) {
            if (Float.parseFloat(prodiKombinasi[i][0]) >= min) {
                dataConfidence[x][0] = prodiKombinasi[i][0];
                dataConfidence[x][1] = prodiKombinasi[i][1];
                dataConfidence[x][2] = prodiKombinasi[i][2];
                dataConfidence[x][3] = prodiKombinasi[i][3];
                dataConfidence[x][4] = prodiKombinasi[i][4];
                x++;
            }
        }

    }

    public void setData() {
        dataFinal = new String[dataConfidence.length][5];
        for (int i = 0; i < dataConfidence.length; i++) {
            dataFinal[i][0] = dataConfidence[i][0];
            dataFinal[i][1] = dataConfidence[i][1];
            dataFinal[i][2] = String.valueOf((Float.parseFloat(dataConfidence[i][2]) / (float) database.getPendaftar().length) * 100);
            for (int j = 0; j < database.getProdi().length; j++) {
                if (dataConfidence[i][3].equals(database.getProdi()[j][0])) {
                    dataFinal[i][3] = database.getProdi()[j][1];
                }
                if (dataConfidence[i][4].equals(database.getProdi()[j][0])) {
                    dataFinal[i][4] = database.getProdi()[j][1];
                }
            }
        }
        float min = Float.parseFloat(txtMin.getText());
        int jumlah = 0;
        for (int i = 0; i < dataFinal.length; i++) {
            if (Float.parseFloat(dataFinal[i][2]) >= min) {
                jumlah += 1;
            }
        }
        String[][] dump = new String[jumlah][5];
        int x = 0;
        for (int i = 0; i < dataFinal.length; i++) {
            if (Float.parseFloat(dataFinal[i][2]) >= min) {
                dump[x][0] = dataFinal[i][0];
                dump[x][1] = dataFinal[i][1];
                dump[x][2] = dataFinal[i][2];
                dump[x][3] = dataFinal[i][3];
                dump[x][4] = dataFinal[i][4];
                x++;
            }
        }
        dataFinal = null;
        dataFinal = dump.clone();
    }

    public double banyakKombinasi(int pendaftar) {
        double a = faktorial(pendaftar);
        double b = faktorial(2);
        double c = faktorial(pendaftar - 2);

        double sum = a / (b * c);
        return sum;

    }

    public double faktorial(int nilai) {
        double sum = 1;
        for (int i = 0; i < nilai; i++) {
            sum *= (i + 1);
        }
        return sum;
    }

    public int keputusan() {
        int jmlProdi1 = 0;
        int jmlProdi2 = 0;

        for (int i = 0; i < dataFinal.length; i++) {

            if (dataFinal[i][3].substring(0, 5).equals("PEND. ")) {
                jmlProdi1 += 1;
            } else {
                jmlProdi2 += 1;
            }
            int a = 0;
        }

        if (jmlProdi1 > jmlProdi2) {
            return 1;
        } else if (jmlProdi2 > jmlProdi1) {
            return 2;
        } else {
            return 0;
        }
    }

    public float getSupport(String a) {

        for (int i = 0; i < database.getProdi().length; i++) {
            if (database.getProdi()[i][0].equals(a)) {
                return Float.parseFloat(database.getProdi()[i][2]);
            }
        }

        return 0;
    }

    public void tampilData() {
        String tampil = new String();

        for (int i = 0; i < dataFinal.length; i++) {
            if (Float.parseFloat(dataFinal[i][0]) >= Float.parseFloat(txtConf.getText())) {
                tampil += "Jika Memilih " + dataFinal[i][3] + " Maka akan memilih " + dataFinal[i][4]
                        + " dengan Support " + dataFinal[i][2]
                        + "% dan confidence " + new DecimalFormat("#.##").format(Float.parseFloat(dataFinal[i][0])) + "% <br><br>";
            } if (Float.parseFloat(dataFinal[i][1]) >= Float.parseFloat(txtConf.getText())) {
                tampil += "Jika Memilih " + dataFinal[i][4] + " Maka akan memilih " + dataFinal[i][3]
                        + " dengan Support " + dataFinal[i][2]
                        + "% dan confidence " + new DecimalFormat("#.##").format(Float.parseFloat(dataFinal[i][1])) + "% <br><br>";
            }
        }
        String tagBuka = "<html> <head></head><body><p>";
        String tagTutup = "</p></body></html>";
        jTextPane1.setText(tagBuka + tampil + tagTutup);

        switch (keputusan()) {
            case 0:
                jTextField3.setText("Pilihan pertama cenderung sama-sama memilih prodi pendidikan dan non-pendidikan");
                break;
            case 1:
                jTextField3.setText("Pilihan Pertama cenderung memilih prodi kependidikan");
                break;
            case 2:
                jTextField3.setText("Pilihan pertama cenderung memilih prodi non kependidikan");
                break;
        }
    int a=0;
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        txtMin = new javax.swing.JTextField();
        txtConf = new javax.swing.JTextField();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextPane1 = new javax.swing.JTextPane();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jTextField3 = new javax.swing.JTextField();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Analisis Apriori");

        jLabel2.setText("Min. Supoort (%)");

        jLabel3.setText("Min. Confidence (%)");

        jButton1.setText("PROSES");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jTextPane1.setContentType("text/html");
        jScrollPane2.setViewportView(jTextPane1);

        jLabel4.setText("Rule yang Terbentuk");

        jLabel5.setText("Keputusan");

        jButton2.setText("Keluar");

        jButton3.setText("Menu utama");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                        .addGap(53, 53, 53)
                        .addComponent(jTextField3, javax.swing.GroupLayout.DEFAULT_SIZE, 394, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                        .addGap(162, 162, 162)
                        .addComponent(jLabel1))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                        .addGap(25, 25, 25)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(txtMin, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(56, 56, 56)
                                .addComponent(txtConf, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(40, 40, 40)
                                .addComponent(jButton1))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addGap(32, 32, 32)
                                .addComponent(jLabel3))
                            .addComponent(jButton3)))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                        .addGap(41, 41, 41)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 406, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                        .addGap(190, 190, 190)
                        .addComponent(jLabel4)))
                .addContainerGap(104, Short.MAX_VALUE))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(221, 221, 221)
                .addComponent(jLabel5)
                .addContainerGap(279, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(390, Short.MAX_VALUE)
                .addComponent(jButton2)
                .addGap(98, 98, 98))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtMin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtConf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1))
                .addGap(45, 45, 45)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel5)
                .addGap(13, 13, 13)
                .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2)
                    .addComponent(jButton3))
                .addContainerGap(29, Short.MAX_VALUE))
        );

        jScrollPane1.setViewportView(jPanel1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 446, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    setMinSupport();
        setKombinasi();
        setConfidence();
        setMinConfidence();
        setData();
        tampilData();
    
}//GEN-LAST:event_jButton1ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(analisis.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(analisis.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(analisis.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(analisis.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new analisis().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextPane jTextPane1;
    private javax.swing.JTextField txtConf;
    private javax.swing.JTextField txtMin;
    // End of variables declaration//GEN-END:variables

     public void setDatabase(Database database) {
        this.database = database;
        dataPemilih = new float[database.getProdi().length];
        setDataPemilih();
        int a = 0;
    }
    
    
}
